package rsj.util;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author yuanweijie
 * @date 2018/8/10
 */
public class QuartzManager {
	
	private static Logger logger = LoggerFactory.getLogger(QuartzManager.class);
	private static SchedulerFactory sf=new StdSchedulerFactory();
	//修改job调度时间
	public static void resched(Long taskId,String expression) throws SchedulerException{
		logger.info("----------- QuartzManager:resched taskid="+taskId+",expression="+expression);
		TriggerKey triggerKey = TriggerKey.triggerKey("trig"+taskId,"group"+taskId);
		 Scheduler scheduler = sf.getScheduler(); 
		//获取trigger，即在spring配置文件中定义的 bean id="myTrigger"
		CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
		try {
			logger.info("CronTrigger before rescheduleJob;StartTime="+trigger.getStartTime()+",CronExpression="+trigger.getCronExpression()+",NextFireTime="+trigger.getNextFireTime()+",PreviousFireTime="+trigger.getPreviousFireTime()+",EndTime="+trigger.getEndTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		//表达式调度构建器
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(expression);
		//当次调度失败不处理,等待下一次正确时间在来处理(CronTrigger Misfire 策略)
		scheduleBuilder.withMisfireHandlingInstructionDoNothing();
		//按新的cronExpression表达式重新构建trigger
		trigger = trigger.getTriggerBuilder()
			.withIdentity(triggerKey)
			.withSchedule(scheduleBuilder)
			.startNow()						//更新上次任务执行时间为当前时间
			.build();
		//按新的trigger重新设置job执行
		scheduler.rescheduleJob(triggerKey, trigger);
		try {
			logger.info("CronTrigger after rescheduleJob;StartTime="+trigger.getStartTime()+",CronExpression="+trigger.getCronExpression()+",NextFireTime="+trigger.getNextFireTime()+",PreviousFireTime="+trigger.getPreviousFireTime()+",EndTime="+trigger.getEndTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info("**********************");
	}
	/**
	 * 创建job调度
	 * @param taskId
	 * @param expression
	 * @throws SchedulerException
	 */
	public static void sched(Long taskId, String expression, Job job) throws SchedulerException{
		 JobDetail jobDetail= JobBuilder.newJob(job.getClass())
	                .withIdentity("job"+taskId,"group"+taskId)
	                .build();
	        jobDetail.getJobDataMap().put("taskId", taskId);
	       // 表达式调度构建器
	        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder
	          .cronSchedule(expression);
	        Trigger trigger= TriggerBuilder
	                .newTrigger()
	                .withIdentity("trig"+taskId,"group"+taskId)
	                .startNow()
	                .withSchedule(scheduleBuilder)
	                .build();
	        Scheduler sched = sf.getScheduler(); 
	        sched.scheduleJob(jobDetail,trigger);
	        sched.start();
	}
	/**
	 * 删除job调度
	 * @param taskId
	 * @throws SchedulerException
	 */
	public static void deleteJob(Long taskId) throws SchedulerException{
		Scheduler scheduler = sf.getScheduler();
		JobKey jobKey = JobKey.jobKey("job"+taskId, "group"+taskId);
		scheduler.deleteJob(jobKey);
	}
}
